<?php
namespace Web\Controller;
use Think\Controller;
use Web\Logic\RoleLogic;
use Web\Logic\MenuGetLogic;
class RoleController extends BaseController {

	public function index(){
		//实例化Logic
		$logic = new RoleLogic();

		//查询总数量
        $andStr = "";


        //获取是否是超级用户 ，如果不是则不能获取隐藏角色
        if (!in_array("1000", $this->mFunction_list)) {
            $admin_role = M('role_info')->where("FIND_IN_SET('1001',function_id)")->getField('roleid',true);
            $wdata['roleid'] = array('not in',$admin_role);
        }

		$count = $logic->selNum($wdata);
		$Page  = $this->getpage($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show  = $Page->show();// 分页显示输出
		//查询数据
		$rolerows = $logic->indexList($wdata, $Page);
        for($i = 0; $i < count($rolerows);$i++){
            if (!in_array("1002", $this->mFunction_list)) {

                $rolerows[$i]['is_set'] = 1;
            } else{
                $rolerows[$i]['is_set'] = 1;
            }
        }
		$this->assign('rolelist',$rolerows);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
        $this->assign("admin_role_id",$admin_role_id);
		$this->display(); // 输出模板
	}

	/**
	 * @desc 添加角色
	 * @param rolename 角色名
	 * @param notes 备注
	 */
	public function add()
	{
		//实例化 logic
		$logic = new RoleLogic();
		//获取提交隐含域
		$subs = $_REQUEST['sub'];
		//接受角色名
		$data['rolename'] = $_REQUEST['rolename'];
		//接受 备注
		$data['notes'] = $_REQUEST['notes'];
		if($subs)
		{
			//判断角色名是否存在
			$row  = $logic->selNum($data);
			if($row >0)
			{
				$this ->error('角色名已存在');
				exit;
			}
			else
			{
				$data['updatedatetime'] = date("Y-m-d H:i:s");
				$r = $logic->addDataSql($data);
				//判断是否添加成功
				if($r){
					$this->success('数据保存成功','index');
					exit;
				}
			}

		}
		$this->display();
	}

	/**
	 * @desc 角色删除
	 * @param roleid 角色id
	 */
	public function deletes()
	{
		//接受 roleid
		$roleid = $_REQUEST['id'];
		//实例化 logic

		$logic = new RoleLogic();
		$des3 = $logic->delDataSql($roleid);
		if($des3)
		{
			$this->success('删除成功', "index");
		}
	}

	/**
	 * @desc 角色修改
	 * @param roleid 角色id
	 */
	public function edit()
	{
		//实例化 logic
		$logic = new RoleLogic();
		//接受 角色id
		$roleid = $_REQUEST['id'];

		//接受表单提交
		$sub = $_REQUEST['sub'];
		if($sub)
		{
			//接受修改信息
			$data['rolename'] = $_REQUEST['rolename'];
			$data['isforbidden'] = $_REQUEST['isforbidden'];
			$data['notes'] = $_REQUEST['notes'];
			$data['updatedatetime'] = date("Y-m-d H:i:s");
			$r = $logic->editDataSql($data, $roleid);
			if($r)
			{
				$this ->success('修改成功','index');
			}
		} else{

		//查询信息
		$rolelist = $logic->selOneSql($roleid);
		$this->assign('rolelist',$rolelist);
		$this->display();

        }
	}

	/**
	 * 设置角色栏目权限
	 */
	public function rolemeun(){
		//实例化 logic
		$logic = new RoleLogic();
		$MenuLogic = new MenuGetLogic();
		//接受参数
		$id = $_REQUEST['roleid'];
		$rolename = $_REQUEST['rolename'];
		$rolemenu = $_REQUEST['rolemenu'];
		$sub = $_REQUEST['sub'];
		//保存数据
		if($sub){
			//数据
			$data['menuroot'] = implode(",", $rolemenu);
			$data['times'] = date("Y-m-d H:i:s");
			$r = $logic->saveRoleMenu($data,$id);
		    if($r){
				$this ->success('保存成功','index');
				exit;
			}
		}

		//查询角色权限
		$roleMenus = $logic->selRoleMenu($id);


		//查询所有栏目
        $session_role_id  = session('roleid');
        $set_role_menu_id = null;
        $mFunction_id_list = explode(",",$this->mFunction_id);

        //判断是否是超级管理员
        if(!in_array("1000",$mFunction_id_list)){
            $set_role_menu_id = $logic->selRoleMenu($session_role_id);
        }


        //只能设置给他分配的权限
		$menuList = $MenuLogic->dealWithMenuForRoleSet($set_role_menu_id);
		$rMenu = $logic->roleMenuDiv($menuList[1],$roleMenus);

		//定义模板变量
		$this->assign('id',$id);
		$this->assign('rolename',$rolename);
		$this->assign('rMenu',$rMenu);
		$this->display();
	}

	//管理role_function_info表
	public function lists(){
		$list = M('role_function_info')->select();
		$this->assign('list', $list);
		$this->display();
	}

	public function editCode($id=0){
		if(IS_POST){
			$act = I('act', 'add');
			if($act == 'add'){
				$rules = array(
					array('code','require','功能代码必填！'), //默认情况下用正则进行验证
					array('code','','功能代码已经存在！',0,'unique',1),
					array('name','require','功能代码名称必填！'), //默认情况下用正则进行验证
					array('name','','功能代码名称已经存在！',0,'unique',1),
				);
			} else {
				$rules = array(
					array('code','require','功能代码必填！'), //默认情况下用正则进行验证
					array('code','','功能代码已经存在！',0,'unique',2),
					array('name','require','功能代码名称必填！'), //默认情况下用正则进行验证
					array('name','','功能代码名称已经存在！',0,'unique',2),
				);
			}

			$RoleFunctionInfo = M('role_function_info');
			if (!$RoleFunctionInfo->validate($rules)->create()){
				 exit('<script>parent.notify("' . $RoleFunctionInfo->getError() . '");</script>');
			} else {
				if($act == 'add'){
					$ins = $RoleFunctionInfo->add();
				} else {
					$ins = $RoleFunctionInfo->save();
				}

				if($ins){
					echo '<script>parent.notify("功能代码保存成功！",function(){parent.location="' . U('lists') . '";});</script>';
				} else {
					echo '<script>parent.notify("功能代码保存失败！");</script>';
				}
			}
		} else {
			$vo = M('role_function_info')->find($id);
			$this->assign('vo', $vo);
			$this->display();
		}
	}

	public function delCode(){
		$ids = I('ids/a');
		$map['id'] = array('in', $ids);
		M('role_function_info')->where($map)->delete();
		echo '1';
	}

	public function roleCode(){
		$roleid = I('roleid',0);
		$function_id = M('role_info')->where(array('roleid'=>$roleid))->getField('function_id');
		$codes = explode(',', $function_id);

		$login_codes = explode(',',$this->mFunction_id);

		if(in_array('1000', $login_codes)){
			$list = M('role_function_info')->select();
		} else {
			$map['code'] = array('in', $login_codes);
			$list = M('role_function_info')->where($map)->select();
		}

		$this->assign('list', $list);
		$this->assign('codes', $codes);
		$this->display();
	}

	public function setRoleCode(){
		if(IS_AJAX){
			$roleid = I('roleid');
			$code = implode(',', I('code/a'));

			//$data['function_id'] = array('exp', "concat_ws(',',function_id,'{$code}')");
			$data['function_id'] = $code;
			$data['times'] = date('Y-m-d H:i:s');
			$RoleInfo = M('role_info');
			$id = $RoleInfo->where(array('roleid'=>$roleid))->getField('id');
			if($id){
				$RoleInfo->where(array('id'=>$id))->save($data);
			} else {
				$data['roleid'] = $roleid;
				$RoleInfo->add($data);
			}
			echo '1';
		}
	}

}